// pages/reservation/reservation.js
import { getStorage} from "../../utils/utils"
import { shareOptions } from "../../utils/access";
import api from "../../config/api";
import request from "../../utils/request";
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
      key: 1,
      name: '附近暂存点',
    }, {
      key: 2,
      name: '预约到家',
    }],
    key: 1,
    stagingList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllStageByRadius();

    // 记忆tab初始化
    const tabKey = options.tabKey || getStorage('tabSelect');
    console.log(tabKey,'tabKey');
    if (tabKey == 2) {
      this.setData({key: +tabKey});
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 获取所有暂存点
   */
  async getAllStageByRadius() {
    const lat = getStorage('latitude')
    const lon = getStorage('longitude')
    if (!lat || !lon) return
    const { province,  city, direct  } = getStorage('addressInfo') || {}

    const res = await request.get(api.stagingPoint, {   
      lon, lat,
      provinceName: province || '',
      cityName: city|| '',
      districtName: direct || ''
    })
    console.log('暂存点数据：', res)
    this.setData({
      stagingList: res?.data || []
    })
  },

  /**
   * 点击tab切换
   */
  handleTabChange(e) {
    const { dataset = {} } = e.currentTarget;
    getStorage('tabSelect', dataset.key);
    this.setData({
      key: dataset.key,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return shareOptions('default', e)
  },
})